package git.soulbgm.mapreduce.wordcount;

import git.soulbgm.mapreduce.utils.DriverUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.GenericOptionsParser;

import java.io.IOException;

/**
 * @author SoulBGM
 * @date 2021/11/18 0:07
 */
public class WordCountDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        if (otherArgs.length < 2) {
            System.err.println("Usage: wordcount <in> [<in>...] <out>");
            System.exit(2);
        }
        Path[] inputPaths = new Path[otherArgs.length - 1];
        for (int i = 0; i < inputPaths.length; i++) {
            inputPaths[i] = new Path(otherArgs[i]);
        }
        Job job = DriverUtil.createJob(conf,
                WordCountDriver.class, WordCountMapper.class, WordCountReducer.class,
                Text.class, IntWritable.class, Text.class, IntWritable.class,
                inputPaths, new Path(otherArgs[otherArgs.length - 1]));

        // 7 提交job
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }

}
